﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SX.BaseService.API.Base;
using SX.BaseService.Service;
using SX.BaseService.Service.Implement.SysUser.ViewModel;
using SX.BaseService.Service.ViewModel;
using SX.Core;
using SX.Core.Attributes;
using SX.Core.Auth;

namespace SX.BaseService.API
{
    [Description("账户管理")]
    [Menu]
    public class SysUserController : BaseController
    {
        private readonly ISysUserService _userService;
        private readonly IWebHostEnvironment _hostEnvironment;
        private readonly ILoginInfo _loginInfo;
        public SysUserController(ISysUserService sysUserService,IWebHostEnvironment webHostEnvironment,ILoginInfo loginInfo)
        {
            _loginInfo = loginInfo;
            _hostEnvironment = webHostEnvironment;
            _userService = sysUserService;
        }

        [HttpGet]
        [Description("查询")]
        public async Task<IApiResult> Query([FromQuery] UserQueryModel model)
        {
            return await _userService.Query(model);
        }

        [HttpPost]
        [Description("新增")]
        public async Task<IApiResult> Add([FromBody] UserAddModel model)
        {
            return await _userService.Add(model);
        }

        [HttpPost]
        [Description("删除")]
        public async Task<IApiResult> Delete([FromBody] List<string> ids)
        {
            return await _userService.Delete(ids);
        }

        [HttpPost]
        [Description("更新")]
        public async Task<IApiResult> Update([FromBody] UserUpdateModel model)
        {
            return await _userService.Update(model);
        }

        [HttpGet]
        [Description("详情")]
        public async Task<IApiResult> Get([FromQuery] string id)
        {
            return await _userService.Get(id);
        }
       
        [HttpGet]
        [Common]     
        [Description("查询单个账户")]
        public async Task<IApiResult> GetBasic()
        {          
            return await _userService.Get(_loginInfo.UserId);
        }

        [HttpPost]
        [Common]
        [Description("密码修改")]
        public async Task<IApiResult> EditPwd([FromBody]JObject parm)
        {
            return await _userService.EditPwd(parm.Value<string>("oldPassword"), parm.Value<string>("newPassword"));

        }

        [HttpPost]
        [Common]
        [Description("修改基本信息")]
        public async Task<IApiResult> UpdateBasic([FromForm]UpdateModel_Basic model)
        {
            return await _userService.UpdateBasic(model);
        }

        [HttpGet]
        [Description("excel导出")]
        public IActionResult ExportExcel()
        {
            var dt = _userService.QueryUserDTable().Result;
            //if (dt == null)
            //throw new Exception("没有需要导出的数据！");
            string fileName =DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            var ms = new MemoryStream();
            var workbook = ExcelHelper.ExportExcelHSSF(dt, "账号,角色,真实姓名,性别,电话,邮箱,地址");
            workbook.Write(ms);
            ms.Flush();
            ms.Seek(0, System.IO.SeekOrigin.Begin);
            var buffer = ms.GetBuffer();
            return File(buffer, "application/vnd.ms-excel", fileName);
        }

        [HttpPost]
        [Description("excel导入")]
        public async Task<IApiResult> ImportExcel([FromForm]IFormFile file)
        {
            return await _userService.ImportData(file);
        }

        [HttpGet]
        [Description("下载数据模板")]
        public async Task<ActionResult> DownLoadExcel()
        {
            string fileName = Path.Combine(_hostEnvironment.WebRootPath, "files", "Template","importuser.xlsx");
            return await Task.Run(()=> PhysicalFile(fileName, "application/octet-stream", HttpUtility.UrlEncode("模板.xlsx", Encoding.UTF8), true));          
        }
    }
}